<template>
  <div class="feature-version__container">
    <div class="table-panel">
      <resource-table
        ref="refResourceTable"
        v-loading="ifLoading"
        :table-columns="tableColumns"
        :showSelection="false"
        operations="edit,delete"
        :operation-column-width="90"
        @queryTable="getList"
      >
        <template #headerLeft>
          <div class="btn-list">
            <div class="btn-item btn-add" style="width: 100px">
              <el-button v-waves type="primary" style="width: 100px" @click="handlerAddNew">
                新增
              </el-button>
            </div>
          </div>
        </template>
        <template #headerRight="props">
          <div class="filter-conditions">
            <!-- <div class="filter-item">
              申请时间：
              <el-date-picker
                v-model="props.queryParameters.timeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="handlerClear"
              >
              </el-date-picker>
            </div>-->
            <div class="filter-item" style="margin-left: 10px">
              数据库名称：
              <el-input
                v-model="props.queryParameters.name"
                size="small"
                style="width: 170px"
                placeholder="请输入数据库名称"
                clearable
                @clear="handlerClear"
              ></el-input>
            </div>
            <div class="filter-item" style="margin-left: 10px">
              数据库类型：
              <el-select
                v-model="props.queryParameters.type"
                size="small"
                style="width: 170px"
                placeholder="请输入数据库类型"
                clearable
                @clear="handlerClear"
              >
                <el-option
                  v-for="item in dbTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </div>
        </template>
        <template #operation="props">
          <el-tooltip class="col-btn-icon" effect="dark" content="修改" placement="top">
            <el-button
              type="text"
              @click="handlerModify(props.rowInfo)"
              icon="el-icon-edit"
              style="font-size: 15px; color: #67c23a"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="col-btn-icon" effect="dark" content="删除" placement="top">
            <el-button
              type="text"
              @click="handlerDelete(props.rowInfo)"
              icon="el-icon-delete"
              style="font-size: 15px; color: #f56c6c"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="col-btn-icon" effect="dark" content="数据图层" placement="top">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-copy-document"
              @click="handlerLayerManage(props.rowInfo)"
              style="font-size: 15px; color: #409eff"
            >
            </el-button>
          </el-tooltip>
        </template>
      </resource-table>
    </div>
    <!-- 数据库信息窗口 -->
    <el-dialog
      :title="formDialogTitle"
      :center="true"
      :visible.sync="formDialogVisible"
      :close-on-click-modal="false"
      class="form-dialog"
      width="40%"
      height="40%"
    >
      <el-form :ref="refFormName" :model="form" :rules="formRules" label-width="120px">
        <el-form-item label-width="0">
          <el-col :span="12">
            <el-form-item label="数据库名" prop="name">
              <el-input v-model="form.name" placeholder="请输入数据库名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据库中文名" prop="cname">
              <el-input v-model="form.cname" placeholder="请输入数据库中文名"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label-width="0">
          <el-col :span="12">
            <el-form-item label="数据库类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择数据库类型" class="form-item">
                <el-option
                  v-for="item in dbTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-form-item>
        <el-form-item label-width="0">
          <el-col :span="12">
            <el-form-item label="数据库服务器" prop="server">
              <el-input v-model="form.server" placeholder="请输入数据库服务器IP"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据库端口" prop="port">
              <el-input v-model="form.port" placeholder="请输入数据库实例端口"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label-width="0">
          <el-col :span="12">
            <el-form-item label="数据库用户名" prop="username">
              <el-input v-model="form.username" placeholder="请输入数据库用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据库密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入数据库密码"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" prop="desc">
          <el-input
            v-model="form.desc"
            type="textarea"
            placeholder="请输入备注"
            :rows="4"
          ></el-input>
        </el-form-item>
        <el-form-item label-width="0">
          <div style="width: 100%; text-align: center">
            <el-button type="primary" @click="handlerSubmit(refFormName)">确定</el-button>
            <el-button @click="handlerCancel(refFormName)">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as moment from 'moment';
import waves from '@/directive/waves';
// import { apiToolApplyListByPage, apiApplyTools, apiApprove } from '@/api/Tools/ToolsApplyList';

export default {
  name: 'FeatureVersion',
  components: {},
  directives: {
    waves,
  },
  data() {
    return {
      // 表格这遮罩
      ifLoading: false,
      // 数据库类型
      dbTypeOptions: [
        {
          label: 'PostgreSQL',
          value: 'PostgreSQL',
        },
        {
          label: 'Oracle11g',
          value: 'Oracle11g',
        },
        {
          label: 'Oracle12C',
          value: 'Oracle12C',
        },
        {
          label: 'MySQL',
          value: 'MySQL',
        },
      ],
      // 表格列配置
      tableColumns: [
        // {
        //   key: 0,
        //   prop: 'id',
        //   align: 'center',
        //   label: '唯一标识',
        //   width: '160',
        // },
        {
          key: 1,
          prop: 'name',
          align: 'center',
          label: '数据库名',
          minWidth: '160',
        },
        {
          key: 2,
          prop: 'cname',
          align: 'center',
          label: '数据库中文名称',
          minWidth: '160',
        },
        {
          key: 3,
          prop: 'type',
          align: 'center',
          label: '数据库类型',
          width: '100',
        },
        {
          key: 4,
          prop: 'server',
          align: 'center',
          label: '所在服务器',
          width: '180',
        },
        {
          key: 5,
          prop: 'port',
          align: 'center',
          label: '数据库端口',
          width: '100',
        },
        {
          key: 6,
          prop: 'username',
          align: 'center',
          label: '用户名',
          width: '160',
        },
        {
          key: 7,
          prop: 'password',
          align: 'center',
          label: '密码',
          width: '160',
        },
        {
          key: 8,
          prop: 'desc',
          align: 'center',
          label: '备注',
          minWidth: '200',
        },
      ],
      // 表格数据
      tableData: [],
      // 表单ref
      refFormName: 'dbForm',
      // 表单标题
      formDialogTitle: '',
      // 表单显隐
      formDialogVisible: false,
      // 表单数据
      form: {
        id: null,
        name: '',
        cname: '',
        type: '',
        server: '',
        port: '',
        username: '',
        password: '',
        desc: '',
      },
      // 表单校验规则
      formRules: {
        name: [{ required: true, message: '请输入数据库名', trigger: 'blur' }],
        cname: [{ required: true, message: '请输入数据库中文名', trigger: 'blur' }],
        type: [{ required: true, message: '请选择数据库类型', trigger: 'blur' }],
        server: [{ required: true, message: '请输入数据库服务器', trigger: 'blur' }],
        port: [{ required: true, message: '请输入数据库端口号', trigger: 'blur' }],
        username: [{ required: true, message: '请输入数据库用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入数据库密码', trigger: 'blur' }],
      },
    };
  },
  mounted() {},
  methods: {
    /**
     * 获取表格数据
     */
    getList(queryParameters, callback) {
      const vm = this;
      // const { name, timeRange, pageSize, page } = queryParameters;
      // let startTime = null;
      // let endTime = null;
      // if (timeRange && timeRange.length === 2) {
      //   startTime = moment(timeRange[0]).format('YYYY-MM-DD HH:mm:ss');
      //   endTime = moment(timeRange[1]).format('YYYY-MM-DD HH:mm:ss');
      // }
      vm.ifLoading = true;
      const timer = setTimeout(() => {
        clearTimeout(timer);
        // MockData
        const mockData = [
          {
            id: 'dsad-ewrw-vvsd-rtra1',
            name: 'zyk_db_xzc',
            cname: '专用库-现状层',
            type: 'PostgreSQL',
            server: '192.168.3.89',
            port: '5432',
            username: 'zyk_db_xzc_username',
            password: 'dsafsagadsvfwretren.',
            desc: '专用库-现状层，包含了历年变更调查调查、地理国情等',
          },
          {
            id: 'dsad-ewrw-vvsd-rtra2',
            name: 'zyk_db_fgc',
            cname: '专用库-覆盖层层',
            type: 'PostgreSQL',
            server: '192.168.3.90',
            port: '5432',
            username: 'zyk_db_fgc_username',
            password: 'dafcvxvzvbgertehjh.',
            desc: '专用库-地表覆盖层',
          },
          {
            id: 'dsad-ewrw-vvsd-rtra3',
            name: 'zyk_db_glc',
            cname: '专用库-管理层',
            type: 'PostgreSQL',
            server: '192.168.10.126',
            port: '1521',
            username: 'zyk_db_glc_username',
            password: 'dafcvxvzvbgertehjh.',
            desc: '专用库-管理层，包含征地、供地、储备、不动产宗地房屋等',
          },
          {
            id: 'dsad-ewrw-vvsd-rtra4',
            name: 'pg_db_wpzf',
            cname: '卫片执法业务库',
            type: 'PostgreSQL',
            server: '192.168.3.90',
            port: '5432',
            username: 'pg_db_wpzf_username',
            password: 'wpzf.',
            desc: '业务库，卫片执法系统',
          },
        ];
        vm.ifLoading = false;
        callback({
          items: mockData,
          total: mockData.length,
        });
      }, 1000);

      // 对接接口
      // apiToolApplyListByPage({
      //   name: name,
      //   startTime,
      //   endTime,
      //   page,
      //   pageSize,
      //   statusList: ['1'],
      // })
      //   .then((res) => {
      //     if (res.status === 'success') {
      //       vm.ifLoading = false;
      //       const {
      //         data: { items },
      //       } = res;
      //       const newItems = items.map((item) => {
      //         // const { applicant, creatTime, id, purpose } = item;
      //         // return {
      //         //   id,
      //         //   create_time: creatTime,
      //         //   approval_purpose: purpose,
      //         //   applicant_name: applicant.name,
      //         //   applicant_phone: applicant.mobile,
      //         // };

      //         const {
      //           // applicant: { company, identity, region, address },
      //           // address,
      //           // company,
      //           // identity,
      //           // region,
      //           applicantName,
      //           applicantPhone,
      //           creatTime,
      //           id,
      //           purpose,
      //           // resourceCount,
      //           // status,
      //         } = item;
      //         return {
      //           id,
      //           create_time: creatTime,
      //           approval_purpose: purpose,
      //           applicant_name: applicantName,
      //           applicant_phone: applicantPhone,
      //         };
      //       });
      //       res.data.items = newItems;
      //       callback(res.data);
      //     } else {
      //       vm.$message({
      //         type: 'error',
      //         message: res.message,
      //       });
      //     }
      //   })
      //   .catch((err) => {
      //     throw new Error(err);
      //   });
    },
    /**
     * 查询
     */
    handlerQuery() {
      const vm = this;
      const refResourceTable = vm.getRefDom('refResourceTable');
      if (refResourceTable) {
        refResourceTable.queryTable();
      }
    },
    /**
     * 添加数据
     */
    handlerAddNew() {
      const vm = this;
      vm.formDialogTitle = '新增数据库信息';
      vm.resetFormFields();
      vm.formDialogVisible = true;
    },
    /**
     * 修改数据
     */
    handlerModify(row) {
      const vm = this;
      vm.formDialogTitle = '修改数据库信息';
      vm.resetFormFields();
      vm.form = row;
      vm.formDialogVisible = true;
    },
    /**
     * 删除数据
     */
    handlerDelete() {
      const vm = this;
      vm.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          vm.$message({
            type: 'success',
            message: '删除成功!',
          });
        })
        .catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    /**
     * 取消提交
     */
    handlerCancel() {
      const vm = this;
      vm.dialogClose();
    },
    /**
     * 提交审批信息
     */
    handlerSubmit(formName) {
      const vm = this;
      vm.$refs[formName].validate((valid) => {
        if (valid) {
          const { id } = vm.form;
          if (id === null) {
            // 新增
            vm.$message.success('新增成功');
            vm.dialogClose();
            return;
          }
          // 修改
          vm.$message.success('修改成功');
          vm.dialogClose();

          // if (status === '' || approval_opinion === '') {
          //   vm.$message.error('请填写审批意见并审批');
          //   return;
          // }

          // apiApprove({
          //   approvalIds: [id],
          //   status,
          //   opinion: approval_opinion,
          // })
          //   .then((res) => {
          //     const { status, message } = res;
          //     if (status === 'success') {
          //       vm.$message.success('审批意见已提交');

          //       vm.handlerQuery();
          //     } else {
          //       vm.$message.error(message);
          //     }
          //   })
          //   .catch((err) => {
          //     console.error(err);
          //   });
        } else {
          vm.$message.error('请正确填写表单项');
          return false;
        }
      });
    },
    /**
     * 检索条件清空
     */
    handlerClear() {
      const vm = this;
      vm.handlerQuery();
    },
    /**
     * 弹窗关闭
     */
    dialogClose() {
      const vm = this;
      vm.resetFormFields();
      vm.formDialogVisible = false;
      vm.handlerQuery();
    },
    /**
     * 根据ref获取dom元素
     */
    getRefDom(refName) {
      const vm = this;
      return vm.$refs[refName];
    },
    /**
     * 重置表单字段并清空非法校验
     */
    resetFormFields() {
      const vm = this;
      const elForm = vm.$refs[vm.refFormName];
      if (elForm) {
        elForm.resetFields();
      }
    },
    /**
     * 图层清单
     */
    handlerLayerManage(row) {
      const vm = this;
      vm.$router.push({ path: `/data-layers` });
    },
  },
};
</script>

<style lang="scss" scoped>
.feature-version__container {
  height: calc(100vh - 85px);
  overflow: hidden;
  background-color: #f2f2f2;
  .content {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 1);
    padding: 15px;
    display: flex;
    flex: 1;
    flex-direction: column;
    .panel-header {
      height: 40px;
    }
    .panel-content {
      flex: 1;
      height: calc(100% - 80px);
      padding: 12px 0;
    }
    .panel-footer {
      height: 40px;
    }
  }

  .search-conditions {
    display: flex;
    flex-direction: column;
    font-size: small;
    .condition-item {
      display: flex;
      align-items: center;
      margin-bottom: 15px;
      .item-label {
        width: 206px;
        text-align: right;
        padding-right: 10px;
      }
      .item-value {
        flex: 1;
      }
    }
  }

  .search-func {
    padding: 0px 10px;
    .btn-query {
      display: inline-block;
      width: 50%;
      padding-right: 10px;
    }
    .btn-reset {
      display: inline-block;
      width: 50%;
      padding-left: 10px;
    }
  }

  .panel-header {
    .table-func {
      width: 28%;
      .table-func-btn {
        display: inline-block;
        padding: 0px 10px;
        width: 25%;
      }
      .btn-add {
        padding-left: 0px;
      }
    }
  }

  .panel-content {
    ::v-deep .table-head-cell-style {
      text-align: center !important;
      color: rgba(100, 100, 100);
    }
    ::v-deep .table-head-cell-style {
      color: rgb(0, 0, 0);
      background: rgb(215, 215, 215);
    }
  }

  .panel-footer {
    display: flex;
    align-items: flex-end;
  }

  .btn-action {
    width: 100%;
    .btn-modify {
      display: inline-block;
      width: 50%;
      padding: 0px 10px;
    }
    .btn-approval {
      display: inline-block;
      width: 50%;
      padding: 0px 10px;
    }
  }

  // 临时
  .table-panel {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 1);
    // padding: 15px 10px;
  }

  .form-dialog {
    ::v-deep .el-dialog__header {
      text-align: center;
    }
    ::v-deep .el-dialog__body {
      padding: 20px !important;
      padding-top: 5px !important;
    }
    ::v-deep .normal-table-header-cell {
      color: rgb(0, 0, 0);
      background: rgb(215, 215, 215);
      font-weight: bold;
    }
  }

  ::v-deep .normal-table-header-cell {
    color: rgb(0, 0, 0);
    background: rgb(215, 215, 215);
    font-weight: bold;
  }

  .btn-list {
    .btn-item {
      display: inline-block;
      .el-button {
        // background: #169bd5;
        // border-color: #169bd5;
      }
    }
  }
  ::v-deep .header-search-button {
    // background: #169bd5;
    // border-color: #169bd5;
  }
  ::v-deep .col-btn-item {
    display: none;
  }
}

.form-dialog {
  .form-panel {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
  }
  .approval-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    .approval-list-item {
      height: 40px;
      line-height: 40px;
      flex: 1;
      display: flex;
      .label {
        width: 13%;
        text-align: center;
      }
      .value {
        flex: 1;
      }
    }
    .permission-approval {
      height: 40px;
      line-height: 40px;
    }
  }
  .approval-action {
    height: 60px;
    line-height: 60px;
    text-align: center;
    .btn-submit {
      width: 150px;
      background: #169bd5;
      border-color: #169bd5;
    }
  }

  ::v-deep .el-dialog__body {
    padding-bottom: 10px;
  }
}

.filter-conditions {
  .filter-item {
    display: inline-block;
    .el-button {
      background: #169bd5;
      border-color: #169bd5;
    }
  }
}

.form-item {
  width: 100%;
}
</style>
